<?php

namespace app\admin\controller;

use app\service\CurriculumService;

class Curriculum extends Base
{
    /**
     * 列表
     * @return \think\response\Json
     */
    public function curriculum_list()
    {
        if (request()->isPost()) {
            $data = input('');
            $page = input('page', 1);
            $size = input('size', 20);
            $where = [];
            if ($data['name']) {
                $where['name'] = ['like', "%{$data['name']}%"];
            }
            $list = CurriculumService::S()->queryPage($where, $page, $size);
            $total = CurriculumService::S()->queryCount($where);
            return returnJson(200, '', compact('page', 'size', 'list', 'total'));
        }
    }

    /**
     * 编辑添加
     * @return \think\response\Json
     */
    public function curriculum_edit()
    {
        if (request()->isPost()) {
            $data = input('');
            if (!$data['name']) {
                return returnJson(202, '请填写姓名');
            }
            if (!$data['desc']) {
                return returnJson(202, '请填写简介');
            }
            if (!$data['content']) {
                return returnJson(202, '请填写教练员详情');
            }
            $save_data = [
                'name' => $data['name'],
                'desc' => $data['desc'],
                'content' => $data['content'],
                'pic' => $data['pic'],
                'update_time' => time(),
                'ord' => $data['ord']
            ];
            if ($data['id']) {
                $where = [
                    'id' => $data['id']
                ];
                $res = CurriculumService::S()->querySave($save_data, $where);
            } else {
                $save_data['create_time'] = time();
                $res = CurriculumService::S()->querySave($save_data);
            }
            if ($res) {
                return returnJson(200, '编辑成功');
            } else {
                return returnJson(202, '编辑失败');
            }
        }
    }


    /**
     * @return \think\response\Json
     */
    public function del_curriculum()
    {
        $input = input();
        if (!$input['id']) {
            return returnJson(1, 'error');
        }
        \app\common\model\Curriculum::destroy(['id' => $input['id']]);
        return returnJson(0, '删除成功');
    }


}

